package demo3;

import java.util.ArrayList;
import java.util.Scanner;

public class ActivitySelection {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入活动数量 n: ");
        int n = scanner.nextInt();

        int[] startTimes = new int[n];
        int[] endTimes = new int[n];

        for (int i = 0; i < n; i++) {
            System.out.println("活动 " + (i + 1) + ":");
            System.out.print("请输入活动 " + (i + 1) + " 的开始时间: ");
            startTimes[i] = scanner.nextInt();
            System.out.print("请输入活动 " + (i + 1) + " 的结束时间: ");
            endTimes[i] = scanner.nextInt();
        }

        ArrayList<Integer> selected = new ArrayList<>();
        selected.add(0);
        int lastEndTime = endTimes[0];

        for (int i = 1; i < n; i++) {
            if (startTimes[i] >= lastEndTime) {
                selected.add(i);
                lastEndTime = endTimes[i];
            }
        }

        System.out.println("\n被选中的活动安排如下：");
        System.out.println("活动号\t开始时间\t结束时间");
        System.out.println("------------------------------");

        for (int i : selected) {
            System.out.printf("%d\t\t%d\t\t%d%n",(i + 1), startTimes[i], endTimes[i]);
        }
        scanner.close();
    }
}